package edu.mobbuzz.page.menu.rsschannel;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.RSSChannel;
import edu.mobbuzz.page.menu.BaseMenu;
import edu.mobbuzz.page.menu.MainMenu;
import edu.mobbuzz.renderer.RSSChannelRenderer;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.string.ModeString;
import java.util.Vector;

/**
 *
 * @author irw
 */
public class RSSChannelMenu extends BaseMenu{
    private Form rssChannelForm;
    private Container rssChannelPanel;
    private Label menuTitle;
    private DefaultListModel rssChannelListModel;
    private List rssChannelList;
    private Vector rssChannelArray;
    private Command backCmd;
    private Command deleteCmd;
    private Command deleteAllCmd;
    private Command editCmd;
    private Command newCmd;
    private Command detailCmd;
    
    public RSSChannelMenu(){
        rssChannelForm = new Form();
        rssChannelPanel = new Container();        
        menuTitle = new Label(getMenuTitle());
        
        rssChannelListModel = new DefaultListModel(displayRSSChannelList());
        
        
        detailCmd = new Command("Detail") {
            public void actionPerformed(ActionEvent evt) {
                rssChannelDetail();
            }
        };
        
        newCmd = new Command("New RSS Channel") {
            public void actionPerformed(ActionEvent evt) {
               subscribeRSSChannel(ModeString.NEW);
            }
        };
        
        editCmd = new Command("Edit RSS Channel") {
            public void actionPerformed(ActionEvent evt) {
               subscribeRSSChannel(ModeString.EDIT);
            }
        };
        
        deleteCmd = new Command("Delete RSS Channel") {
            public void actionPerformed(ActionEvent evt) {
               deleteRSSChannel();
            }
        };
        
        deleteAllCmd = new Command("Delete All RSS Channel"){
            public void actionPerformed(ActionEvent evt) {
               deleteAllRSSChannel();
            }
        };
        
        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                backToRSSChannelMenu();
            }
        };
        
    }
    public void showPage() {
        
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);    
        
        rssChannelPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));                 
        rssChannelPanel.addComponent(menuTitle);
        rssChannelPanel.addComponent(createList(rssChannelListModel, List.VERTICAL, new RSSChannelRenderer()));
        
        rssChannelForm.setLayout(new BorderLayout());       
        rssChannelForm.addComponent(BorderLayout.CENTER, rssChannelPanel);
        
        rssChannelForm.addCommand(backCmd);
        
        if(rssChannelListModel.getSize()!=0){                    
            rssChannelForm.addCommand(deleteAllCmd);
            rssChannelForm.addCommand(deleteCmd);
            rssChannelForm.addCommand(editCmd);
            
        }
        
        rssChannelForm.addCommand(newCmd);
        
        if(rssChannelListModel.getSize()!=0){
            rssChannelForm.addCommand(detailCmd);
        }       
        
        rssChannelForm.setCommandListener(getActionlistener());
        rssChannelForm.setScrollable(false);
        rssChannelForm.show();
    }
    

    private List createList(DefaultListModel rssChannelListModel, int orientation, ListCellRenderer renderer) {
        rssChannelList = new List(rssChannelListModel){
            public void keyPressed(int keyCode){
               gameActionPerformed(keyCode);
            }
        };        
        rssChannelList.getStyle().setBgTransparency(0);
        rssChannelList.getStyle().setBorder(Border.getEmpty());
        rssChannelList.setListCellRenderer(renderer);
        rssChannelList.setOrientation(orientation);
        return rssChannelList;
    }

    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);
        
        if(game == Display.GAME_FIRE){
            if(rssChannelListModel.getSize()==0){
                subscribeRSSChannel(ModeString.NEW);
            }else{
                rssChannelDetail();
            }
        }else if(game == Display.GAME_DOWN){
            System.out.println("DOWN " + rssChannelList.getSelectedIndex());
            if(rssChannelList.getSelectedIndex()<(rssChannelList.size()-1)){
               rssChannelList.setSelectedIndex(rssChannelList.getSelectedIndex()+1);
            }else{
                rssChannelList.setSelectedIndex(0);
            }
           
        }else if(game == Display.GAME_UP){
            System.out.println("UP " + rssChannelList.getSelectedIndex());
            if(rssChannelList.getSelectedIndex()!=0){
                rssChannelList.setSelectedIndex(rssChannelList.getSelectedIndex()-1);
            }else{
                rssChannelList.setSelectedIndex(rssChannelList.size()-1);
            }           
        }
    }
    
    public Vector displayRSSChannelList() {
        
        rssChannelArray = new Vector();          
        
        if(getRssChannelRS().readRSRSSChannel()){
            for(int i=0;i<getRssChannelRS().getNbRSSChannel();i++){
                rssChannelArray.addElement(new RSSChannel(getRssChannelRS().rssChannelArr[i].getRecId(),
                                                          getRssChannelRS().rssChannelArr[i].getTitle(),
                                                          getRssChannelRS().rssChannelArr[i].getSource(),
                                                          getRssChannelRS().rssChannelArr[i].getLabel()));
                
            }
        }else{
            System.out.println("RS Contact Gagal dibuka");
        } 
        
        
        return rssChannelArray;
    }
    
    public void rssChannelDetail(){
        
    }
    
    public void subscribeRSSChannel(int mode) {
       
    }
    
    public void deleteRSSChannel(){
        TextArea body = new TextArea("This is the body of the alert....", 2, 5);
        body.setGrowByContent(true);
        Command noCommand = new Command("No");
        Command yesCommand = new Command("Yes"){
            public void actionPerformed(ActionEvent evt) {
                getRssChannelRS().deleteRSSChannel(rssChannelList.getSelectedIndex());                 
                rssChannelListModel.removeItem(rssChannelList.getSelectedIndex());
                if(rssChannelListModel.getSize()==0){
                    rssChannelForm.removeCommand(deleteCmd);
                    rssChannelForm.removeCommand(deleteAllCmd);
                    rssChannelForm.removeCommand(editCmd);
                    rssChannelForm.removeCommand(detailCmd);
                }
            }
        };
        Dialog.show("", body, new Command[]{noCommand, yesCommand}, Dialog.TYPE_CONFIRMATION, null,0,null);
               
    }
    
    public void deleteAllRSSChannel(){
        TextArea body = new TextArea("This is the body of the alert....", 2, 5);
        body.setGrowByContent(true);
        Command noCommand = new Command("No");
        Command yesCommand = new Command("Yes"){
            public void actionPerformed(ActionEvent evt) {
                getRssChannelRS().deleteRSSChannel(rssChannelList.getSelectedIndex());                 
                rssChannelListModel.removeAll();
                if(rssChannelListModel.getSize()==0){                    
                    rssChannelForm.removeCommand(deleteCmd);
                    rssChannelForm.removeCommand(deleteAllCmd);
                    rssChannelForm.removeCommand(editCmd);
                    rssChannelForm.removeCommand(detailCmd);
                }
                
                
            }
        };
        Dialog.show("", body, new Command[]{noCommand, yesCommand}, Dialog.TYPE_CONFIRMATION, null,0,null);
               
    }
    
    public void backToRSSChannelMenu(){
        MainMenu rssChannelMenu = new MainMenu();
        rssChannelMenu.setExitCmd(getExitCmd());
        rssChannelMenu.setActionlistener(getActionlistener());
        rssChannelMenu.showPage();
    }
        
    public String getMenuTitle() {
        return MenuString.RSSCHANNEL;
    }

}
